﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;
using System.Data;

namespace EnterpriseFund.Controllers
{
    public class MessageDlgController : AdminBaseController
    {
        //
        // GET: /MessageDlg/

        public ActionResult Index(string tableName, string feq)
        {
            ViewData["tableName"] = tableName;
            ViewData["feq"] = feq;
            return View("Index");

        }

        public ActionResult MessageLook()
        {
            BLL.WorkFlowToDo w = new BLL.WorkFlowToDo();

            BLL.PrvFunctList prvFunctListBLL = new BLL.PrvFunctList();
            DataSet ds = prvFunctListBLL.getFunctList(OperateSession.get(GlobalConstant.PARAM_LOGINID), GlobalConstant.ENTITYNO);
            List<Model.WorkFlowToDo> wl = w.GetModelList("FStatus='M'");
            int count = 0;
            Model.WorkFlowToDo wo = new Model.WorkFlowToDo();
            if (wl.Count > 0)
            {
                foreach (Model.WorkFlowToDo wft in wl)
                {
                    string pname = IndexController.WorkPrvFunctList(wft.FTableName, wft.FSeq.ToString());
                    if (pname != "")
                    {
                        DataRow[] drmain = ds.Tables[0].Select("FFunctNo='" + pname + "'");
                        if (drmain.Count() > 0)
                        {
                            if (count == 0)
                            {
                                wo = wft;
                            }
                            count++;
                        }

                    }
                }

                wo.FStatus = "Y";
                w.Update(wo);
                BLL.WorkFlowProcess wf = new BLL.WorkFlowProcess();
                Model.WorkFlowProcess mf = wf.GetModel(decimal.Parse(wo.FSourceNo));
                ViewData["counts"] = count - 1;
                return View(mf);
            }
            else
            {
                ViewData["counts"] = "0";
                Model.WorkFlowProcess mf = new Model.WorkFlowProcess();
                return View(mf);
            }
        }

        public ActionResult Save()
        {

            string tableName = Fetch.Post("hidtableName");
           // string no = Fetch.Post("hidid");
            string feq = Fetch.Post("hidfeq");
            try
            {
                BLL.WorkFlowProcess workFlowProcessBLL = new BLL.WorkFlowProcess();
                Model.WorkFlowProcess wpModel = new Model.WorkFlowProcess();
                wpModel.FEntityNo = GlobalConstant.ENTITYNO;
                wpModel.FDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                wpModel.FEmplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                wpModel.FInsIP = GenerateUtil.GetClientIPv4Address();
                wpModel.FNote = Fetch.Post("FNote");
                wpModel.FProcDate = DateTime.Now;
                wpModel.FSeq = int.Parse(feq);
                wpModel.FSourceNo = "-1";
                wpModel.FTableName = tableName;
                Model.WorkFlowToDo wtModel = new Model.WorkFlowToDo();
                wtModel.FEntityNo = GlobalConstant.ENTITYNO;
                wtModel.FSourceNo = "-1";
                wtModel.FSubmitDate = DateTime.Now;
                wtModel.FSubmitDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                wtModel.FSubmitEmplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                wtModel.FSubmitIP = GenerateUtil.GetClientIPv4Address();
                wtModel.FTableName = tableName;
                wpModel.FStatus = "M";
                wtModel.FStatus = "M";
                wtModel.FSeq = int.Parse(feq);
                workFlowProcessBLL.AddMessage(wpModel, wtModel);
                //workFlowProcessBLL.UpdateR(no, nDr[0]["TableKey"].ToString(), nDr[0]["TableFStatus"].ToString(), tableName);
                ViewData["jsAlert"] = "操作成功";
            }
            catch (Exception ex)
            {
                ViewData["jsAlert"] = ex.Message;
            }

            return View("index");
        }

    }
}
